package com.huika.dealontop.control.timelimit.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.support.domain.MemberEvaluateBean;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.DateTimeTool;
import com.huika.dealontop.views.RoundImageView;
import com.huika.dealontop.views.TitleBarHelper;
import com.huika.huikalib.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by huhao on 2015/11/26.
 */
public class MemberEvaluateActicvity extends BaseAct {
    private String evaluateid;
    private MemberEvaluateBean memberEvaluateList;
    private ImageView iv_memberevaluate;
    private TextView tv_name, tv_price, tv_zhoukounum;
    private RatingBar rb_service, rb_enviroment, rb_price, rb_pingfen;
    private TextView tv_service, tv_enviroment, tv_pricenum, tv_pingfennum, tv_username, tv_time, tv_content,
            tv_business_content;
    private RoundImageView riv_icon;
    public DisplayImageOptions options;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_nemberevaluate);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.common_empty, R.string.common_empty, R
                .string.memberevaluate_title);
        titleBarHelper.setOnLeftClickListener(this);
        iv_memberevaluate = (ImageView) findViewById(R.id.memberevaluate_iv_icon);
        tv_name = (TextView) findViewById(R.id.memberevaluate_tv_name);
        tv_price = (TextView) findViewById(R.id.memberevaluate_tv_price);
        tv_zhoukounum = (TextView) findViewById(R.id.memberevaluate_tv_zhekounum);
        rb_pingfen = (RatingBar) findViewById(R.id.memberevaluate_rb_pingfen);
        tv_pingfennum = (TextView) findViewById(R.id.memberevaluate_tv_pingfennum);
        riv_icon = (RoundImageView) findViewById(R.id.memberevaluate_user_icon);
        tv_username = (TextView) findViewById(R.id.memberevaluate_user_name);
        tv_time = (TextView) findViewById(R.id.memberevaluate_user_time);
        rb_price = (RatingBar) findViewById(R.id.rb_memberevaluate_ae);
        rb_enviroment = (RatingBar) findViewById(R.id.rb_environment_ae);
        rb_service = (RatingBar) findViewById(R.id.rb_memberevaluate_serve);
        tv_pricenum = (TextView) findViewById(R.id.tv_memberevaluate_price);
        tv_service = (TextView) findViewById(R.id.tv_memberevaluate_service);
        tv_enviroment = (TextView) findViewById(R.id.tv_memberevaluate_environment);
        tv_content = (TextView) findViewById(R.id.tv_memberevaluate_content);
        tv_business_content = (TextView) findViewById(R.id.tv_memberevaluate_business);
        // 创建头像下载
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.my_infomation_user_icon)
                .showImageForEmptyUri(R.mipmap.my_infomation_user_icon)
                .showImageOnFail(R.mipmap.my_infomation_user_icon).cacheInMemory(true)// 开启内存缓存
                .cacheOnDisk(true) // 开启硬盘缓存
                .resetViewBeforeLoading(false).build();
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        if (intent != null) {
            evaluateid = intent.getStringExtra(Constants.EVALUATE_ID);
        }
        getMemberEvaluateList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMemberEvaluateList();
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.left:
                finish();
                break;
        }
    }

    /**
     * 获取评论中评价详情
     */
    private void getMemberEvaluateList() {
        showLoadingDialog("");
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("evaluateID", evaluateid);
        FormResultRequest<MemberEvaluateBean> request = new FormResultRequest<MemberEvaluateBean>(UrlConstants
                .MEMBEREVALUATEDETAIL, mgetMemberEvaluateList, this, new TypeToken<RequestResult<MemberEvaluateBean>>
                () {
        }.getType());
        request.setRequestParams(ajaxParams);
        executeRequest(request);
    }

    private Response.Listener<RequestResult<MemberEvaluateBean>> mgetMemberEvaluateList = new
            Response.Listener<RequestResult<MemberEvaluateBean>>() {
                @Override
                public void onResponse(RequestResult<MemberEvaluateBean> Result) {
                    dismissLoadingDialog();
                    if (Result.flag == 1) {
                        memberEvaluateList = new MemberEvaluateBean();
                        memberEvaluateList = Result.getRs();
                        getMess(memberEvaluateList);
                    }
                }

            };

    private void getMess(MemberEvaluateBean bean) {
        ImageLoader.getInstance().displayImage(bean.getHeadImage(), riv_icon, options);
        if (!StringUtils.isEmpty(bean.getCouponImage())) {
            ImageLoader.getInstance().displayImage(bean.getCouponImage(), iv_memberevaluate);
        } else {
            iv_memberevaluate.setImageResource(R.mipmap.morenpic);
        }
        tv_name.setText(bean.getProductName());
        tv_price.setText(Math.round(bean.getCouponPrice()) + "");
        tv_zhoukounum.setText(bean.getDiscount() + "");
        tv_pingfennum.setText(Math.round(bean.getProductAvgEvaluate()) + ".0");
        rb_pingfen.setRating(Math.round(bean.getProductAvgEvaluate()));
        tv_username.setText(bean.getNickName());
        tv_time.setText(String.format(DateTimeTool.dmyhmsDateTwo(Long.parseLong(bean.getEvaluateTime()))) + "");
        rb_service.setRating(Math.round(bean.getServices()));
        rb_enviroment.setRating(Math.round(bean.getEnvironment()));
        rb_price.setRating(Math.round(bean.getCost()));
        tv_pricenum.setText(Math.round(bean.getCost()) + ".0");
        tv_service.setText(Math.round(bean.getServices()) + ".0");
        tv_enviroment.setText(Math.round(bean.getEnvironment()) + ".0");
        tv_service.setText(Math.round(bean.getServices()) + ".0");
        tv_content.setText(bean.getEvaluateContent());
        if (!StringUtils.isEmpty(bean.getMerchantReply())) {
            tv_business_content.setVisibility(View.VISIBLE);
            tv_business_content.setText(getString(R.string.memberevaluate_merchant) + bean.getMerchantReply());
        } else {
            tv_business_content.setVisibility(View.GONE);
        }

    }
}
